<template>
  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">登出</el-button>
    </el-header>
    <!-- 主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- 宽度为动态，折叠时为64，展开时为200 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-buton" @click="toggleCollapse">|||</div>
        <!-- 侧边栏区域 -->
        <!-- :unique-opened="true" 每次只能展开唯一的一项， active-text-color="#409EFF" 触发时的颜色, router 开启路由模式-->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <!-- 增加循环 v-for，每一个v-for都提供一个唯一的key属性,":"为动态数据绑定 ,indwx只接收字符串不接收数字-->
          <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i :class="iconObject[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + submenu.path" v-for="submenu in item.children" :key="submenu.id" @click="saveNameStatus('/' + submenu.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ submenu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 有侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 行为区域
export default {
  // 定义组件的私有数据
  data() {
    return {
      // 左侧菜单数据
      menusList: [],
      iconObject: {
        // 定义id对应的图标key：value
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao",
      },
      //是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath:""
    };
  },
  // 定义生命周期函数，
  created() {
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  methods: {
    // 退出函数
    loginOut() {
      // 删除token
      window.sessionStorage.clear();
      // 删除token之后跳转登录页
      this.$router.push("/login");
    },
    // 获取所有菜单,（async，await用于简化promis操作）
    async getMenuList() {
      // 发起get请求，地址是menus，通过结构赋值const{data:res} res接收
      const { data: res } = await this.$http.get("menus");
      // 如果请求状态码为不为200就返回错误信息，其余将data数据返回给menusList
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menusList = res.data;
      console.log(this.menusList);
      console.log(res);
    },
    // 点击按钮实现菜单的折叠和展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 定义保存路径函数  用于保存二级菜单的激活状态
    saveNameStatus(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath=activePath
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  weight: 100%;
}
.el-header {
  background-color: #373d42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-content: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-buton {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
